package com.eff.android.lib.ui;

import android.app.ActionBar;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;

public class ActionBarController {
	
	//Instnace for handle actionbar
	private static ActionBar m_ActionBar = null;
	private Activity m_Activity = null;
	private static ActionBarController m_Instance = null;
	
	public static ActionBarController getInstance()
	{
		if(m_Instance == null)
			m_Instance = new ActionBarController();
		return m_Instance;
	}
	
	/**
	 * Dang ky mot view layout moi cho actionbar
	 * @param resourceID: Layout ID
	 */
	public void registerCustomView(int resourceID)
	{
		setActionBar(m_Activity.getActionBar());
		View v = m_Activity.getLayoutInflater().inflate(resourceID, null);
		if(v != null)
			m_ActionBar.setCustomView(v, getLayouyParams());
	}
	
	public void setActivity(Activity activity)
	{
		m_Activity = activity;
	}
	
	private void setActionBar(ActionBar actionBar)
	{
		m_ActionBar = actionBar;
	}
	
	private void setActionBarAttributes()
	{
		m_ActionBar.setDisplayShowHomeEnabled(false);
		m_ActionBar.setHomeButtonEnabled(false);
		m_ActionBar.setDisplayHomeAsUpEnabled(false);
		m_ActionBar.setDisplayUseLogoEnabled(false);
		m_ActionBar.setDisplayShowTitleEnabled(false);
		m_ActionBar.setDisplayShowCustomEnabled(true);
		m_ActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
	}
	
	private LayoutParams getLayouyParams() {
		LayoutParams params = new LayoutParams(
										LayoutParams.MATCH_PARENT,
										LayoutParams.MATCH_PARENT,
										Gravity.CENTER);
		return params;
	}

	public ActionBar getActionBar()
	{
		setActionBarAttributes();
		return m_ActionBar;
	}
	
	public static ActionBar GetActionBarInstance(Activity activity){
		if(m_ActionBar == null){
			m_ActionBar = activity.getActionBar();
			if(m_ActionBar != null){
				m_ActionBar.setDisplayShowHomeEnabled(false);
				m_ActionBar.setHomeButtonEnabled(false);
				m_ActionBar.setDisplayHomeAsUpEnabled(false);
				m_ActionBar.setDisplayUseLogoEnabled(false);
				m_ActionBar.setDisplayShowTitleEnabled(false);
				m_ActionBar.setDisplayShowCustomEnabled(true);
				m_ActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
			}
		}
		return m_ActionBar;
	}
	
	public void SetActionBarCustomView(ActionBar actionbar, View view){
		actionbar.setDisplayShowHomeEnabled(false);
		actionbar.setHomeButtonEnabled(false);
		actionbar.setDisplayHomeAsUpEnabled(false);
		actionbar.setDisplayUseLogoEnabled(false);
		actionbar.setDisplayShowTitleEnabled(false);
		actionbar.setDisplayShowCustomEnabled(true);
		actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		
		actionbar.setCustomView(view);
	}
}
